home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / malloc / realloc.c < prev    next >
C/C++ Source or Header  |  1994-03-03  |  6KB  |  219 lines

  1. /* Change the size of a block allocated by `malloc'.
  2.    Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.              Written May 1989 by Mike Haertel.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.
  19.  
  20.    The author may be reached (Email) at the address mike@ai.mit.edu,
  21.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  22.  
  23. #ifndef    _MALLOC_INTERNAL
  24. #define _MALLOC_INTERNAL
  25. #include <malloc.h>
  26. #endif
  27.  
  28. #if  (defined (MEMMOVE_MISSING) || \
  29.       !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
  30.  
  31. /* Snarfed directly from Emacs src/dispnew.c:
  32.    XXX Should use system bcopy if it handles overlap.  */
  33. #ifndef emacs
  34.  
  35. /* Like bcopy except never gets confused by overlap.  */
  36.  
  37. static void
  38. safe_bcopy (from, to, size)
  39.      char *from, *to;
  40.      int size;
  41. {
  42.   if (size <= 0 || from == to)
  43.     return;
  44.  
  45.   /* If the source and destination don't overlap, then bcopy can
  46.      handle it.  If they do overlap, but the destination is lower in
  47.      memory than the source, we'll assume bcopy can handle that.  */
  48.   if (to < from || from + size <= to)
  49.     bcopy (from, to, size);
  50.  
  51.   /* Otherwise, we'll copy from the end.  */
  52.   else
  53.     {
  54.       register char *endf = from + size;
  55.       register char *endt = to + size;
  56.  
  57.       /* If TO - FROM is large, then we should break the copy into
  58.      nonoverlapping chunks of TO - FROM bytes each.  However, if
  59.      TO - FROM is small, then the bcopy function call overhead
  60.      makes this not worth it.  The crossover point could be about
  61.      anywhere.  Since I don't think the obvious copy loop is too
  62.      bad, I'm trying to err in its favor.  */
  63.       if (to - from < 64)
  64.     {
  65.       do
  66.         *--endt = *--endf;
  67.       while (endf != from);
  68.     }
  69.       else
  70.     {
  71.       for (;;)
  72.         {
  73.           endt -= (to - from);
  74.           endf -= (to - from);
  75.  
  76.           if (endt < to)
  77.         break;
  78.  
  79.           bcopy (endf, endt, to - from);
  80.         }
  81.  
  82.       /* If SIZE wasn't a multiple of TO - FROM, there will be a
  83.          little left over.  The amount left over is
  84.          (endt + (to - from)) - to, which is endt - from.  */
  85.       bcopy (from, to, endt - from);
  86.     }
  87.     }
  88. }     
  89. #endif    /* Not emacs.  */
  90.  
  91. #define memmove(to, from, size) safe_bcopy ((from), (to), (size))
  92.  
  93. #endif
  94.  
  95.  
  96. #define min(A, B) ((A) < (B) ? (A) : (B))
  97.  
  98. /* Debugging hook for realloc.  */
  99. __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size));
  100.  
  101. /* Resize the given region to the new size, returning a pointer
  102.    to the (possibly moved) region.  This is optimized for speed;
  103.    some benchmarks seem to indicate that greater compactness is
  104.    achieved by unconditionally allocating and copying to a
  105.    new region.  This module has incestuous knowledge of the
  106.    internals of both free and malloc. */
  107. __ptr_t
  108. realloc (ptr, size)
  109.      __ptr_t ptr;
  110.      size_t size;
  111. {
  112.   __ptr_t result;
  113.   int type;
  114.   size_t block, blocks, oldlimit;
  115.  
  116.   if (size == 0)
  117.     {
  118.       free (ptr);
  119.       return malloc (0);
  120.     }
  121.   else if (ptr == NULL)
  122.     return malloc (size);
  123.  
  124.   if (__realloc_hook != NULL)
  125.     return (*__realloc_hook) (ptr, size);
  126.  
  127.   block = BLOCK (ptr);
  128.  
  129.   type = _heapinfo[block].busy.type;
  130.   switch (type)
  131.     {
  132.     case 0:
  133.       /* Maybe reallocate a large block to a small fragment.  */
  134.       if (size <= BLOCKSIZE / 2)
  135.     {
  136.       result = malloc (size);
  137.       if (result != NULL)
  138.         {
  139.           memcpy (result, ptr, size);
  140.           _free_internal (ptr);
  141.           return result;
  142.         }
  143.     }
  144.  
  145.       /* The new size is a large allocation as well;
  146.      see if we can hold it in place. */
  147.       blocks = BLOCKIFY (size);
  148.       if (blocks < _heapinfo[block].busy.info.size)
  149.     {
  150.       /* The new size is smaller; return
  151.          excess memory to the free list. */
  152.       _heapinfo[block + blocks].busy.type = 0;
  153.       _heapinfo[block + blocks].busy.info.size
  154.         = _heapinfo[block].busy.info.size - blocks;
  155.       _heapinfo[block].busy.info.size = blocks;
  156.       /* We have just created a new chunk by splitting a chunk in two.
  157.          Now we will free this chunk; increment the statistics counter
  158.          so it doesn't become wrong when _free_internal decrements it.  */
  159.       ++_chunks_used;
  160.       _free_internal (ADDRESS (block + blocks));
  161.       result = ptr;
  162.     }
  163.       else if (blocks == _heapinfo[block].busy.info.size)
  164.     /* No size change necessary.  */
  165.     result = ptr;
  166.       else
  167.     {
  168.       /* Won't fit, so allocate a new region that will.
  169.          Free the old region first in case there is sufficient
  170.          adjacent free space to grow without moving. */
  171.       blocks = _heapinfo[block].busy.info.size;
  172.       /* Prevent free from actually returning memory to the system.  */
  173.       oldlimit = _heaplimit;
  174.       _heaplimit = 0;
  175.       _free_internal (ptr);
  176.       _heaplimit = oldlimit;
  177.       result = malloc (size);
  178.       if (result == NULL)
  179.         {
  180.           /* Now we're really in trouble.  We have to unfree
  181.          the thing we just freed.  Unfortunately it might
  182.          have been coalesced with its neighbors.  */
  183.           if (_heapindex == block)
  184.             (void) malloc (blocks * BLOCKSIZE);
  185.           else
  186.         {
  187.           __ptr_t previous = malloc ((block - _heapindex) * BLOCKSIZE);
  188.           (void) malloc (blocks * BLOCKSIZE);
  189.           _free_internal (previous);
  190.         }
  191.           return NULL;
  192.         }
  193.       if (ptr != result)
  194.         memmove (result, ptr, blocks * BLOCKSIZE);
  195.     }
  196.       break;
  197.  
  198.     default:
  199.       /* Old size is a fragment; type is logarithm
  200.      to base two of the fragment size.  */
  201.       if (size > (size_t) (1 << (type - 1)) && size <= (size_t) (1 << type))
  202.     /* The new size is the same kind of fragment.  */
  203.     result = ptr;
  204.       else
  205.     {
  206.       /* The new size is different; allocate a new space,
  207.          and copy the lesser of the new size and the old. */
  208.       result = malloc (size);
  209.       if (result == NULL)
  210.         return NULL;
  211.       memcpy (result, ptr, min (size, (size_t) 1 << type));
  212.       free (ptr);
  213.     }
  214.       break;
  215.     }
  216.  
  217.   return result;
  218. }
  219.